/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.painting.resources;

import android.graphics.Canvas;

import com.hercules.android.receiver.AndroidReceiver;
import com.hercules.android.receiver.LogUtil;
import com.hercules.android.receiver.handlers.events.EventRaiser;
import com.hercules.android.receiver.model.commands.XRECommand;
import com.hercules.android.receiver.model.objects.XREResource;
import com.hercules.android.receiver.model.objects.XREView;
import com.hercules.android.receiver.painting.PaintersUtil;
import com.hercules.android.receiver.painting.XREObjectPainterException;
import com.hercules.android.receiver.painting.views.AndroidXREView;

import java.util.logging.Logger;

/**
 * This is base class that will be used to handle resource drawing. It will be extended for every resource and override
 * required methods to display resource on screen.
 * <p>
 * AndroidAssembly1 this class was added in Android Assembly 1
 * <p>
 * <b>Thread safety:</b> It is mutable and not thread safe
 *
 * @author 7realm
 * @version 1.0
 */
public abstract class BaseXREResourcePainter {
    /**
     * Represents the logger.
     * <p>
     * Can be any value, accessed by setter.
     */
    private Logger logger;
    /**
     * Represents corresponding {@link XREView}.
     * <p>
     * Has getter and setter. Can be any value.
     */
    private XREView xreView;
    /**
     * Represents UI android view.
     * <p>
     * Has getter and setter. Can be any value.
     */
    private AndroidXREView androidView;
    /**
     * Represents android receiver.
     * <p>
     * Has getter and setter. Can be any value.
     */
    private AndroidReceiver receiver;
    /**
     * Represents the {@link EventRaiser}, used to raise resource events.
     * <p>
     * Has getter and setter. Can be any value.
     * <p>
     * AndroidAssembly2 new field, with getter and setter
     */
    private EventRaiser eventRaiser;
    /**
     * Represents the corresponding {@link XREResource}.
     * <p>
     * Is initialized at construction. Has getter. Will not change.
     */
    private final XREResource xreResource;

    /**
     * Constructor for class BaseAndroidXREResource.
     *
     * @param xreResource the {@link XREResource}
     * @throws IllegalArgumentException if any argument is null
     */
    protected BaseXREResourcePainter(XREResource xreResource) {
        PaintersUtil.checkNull(xreResource, "xreResource");

        this.xreResource = xreResource;

        logger = LogUtil.getLogger(getClass().getSimpleName());
    }

    /**
     * Performs initialize actions.
     *
     * @throws XREObjectPainterException if error occurs while init procedure
     */
    public void init() throws XREObjectPainterException {
        // empty
    }

    /**
     * Performs clean up.
     *
     * @throws XREObjectPainterException if error occurs while clean up
     */
    public void finish() throws XREObjectPainterException {
        // empty
    }

    /**
     * Handle {@link XRECommand}.
     *
     * @param command the command to handle
     * @throws XREObjectPainterException if error occurs while handling command
     */
    public void handleCommand(XRECommand command) throws XREObjectPainterException {
        // empty
    }

    /**
     * Draw resource on surface.
     *
     * @param canvas the canvas to be used
     */
    public void onDraw(Canvas canvas) {
        // empty
    }

    /**
     * Gets the corresponding {@link XREResource}.
     *
     * @return the corresponding {@link XREResource}
     */
    public XREResource getXREResource() {
        return xreResource;
    }

    /**
     * Gets the UI android view.
     *
     * @return the UI android view
     */
    public AndroidXREView getAndroidView() {
        return androidView;
    }

    /**
     * Sets the UI android view.
     *
     * @param androidView the UI android view
     */
    public void setAndroidView(AndroidXREView androidView) {
        this.androidView = androidView;
    }

    /**
     * Gets the corresponding {@link XREView}.
     *
     * @return the corresponding {@link XREView}
     */
    public XREView getXREView() {
        return xreView;
    }

    /**
     * Sets the corresponding {@link XREView}.
     *
     * @param xreView the corresponding {@link XREView}
     */
    public void setXREView(XREView xreView) {
        this.xreView = xreView;
    }

    /**
     * Gets the android receiver.
     *
     * @return the android receiver
     */
    public AndroidReceiver getReceiver() {
        return receiver;
    }

    /**
     * Sets the android receiver.
     *
     * @param receiver the android receiver
     */
    public void setReceiver(AndroidReceiver receiver) {
        this.receiver = receiver;
    }

    /**
     * Gets the logger.
     *
     * @return the logger
     */
    public Logger getLogger() {
        return logger;
    }

    /**
     * Sets the logger.
     *
     * @param logger the logger
     */
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    /**
     * Gets the {@link EventRaiser}, used to raise resource events.
     *
     * @return the {@link EventRaiser}, used to raise resource events
     */
    public EventRaiser getEventRaiser() {
        return eventRaiser;
    }

    /**
     * Sets the {@link EventRaiser}, used to raise resource events.
     *
     * @param eventRaiser the {@link EventRaiser}, used to raise resource events
     */
    public void setEventRaiser(EventRaiser eventRaiser) {
        this.eventRaiser = eventRaiser;
    }

    /**
     * Get resource's measured width.
     *
     * @return measured width of resource
     */
    public int getMeasuredWidth() {
        if (xreView == null) {
            throw new IllegalStateException("Resource measured width is required, but it is not attached to XREView.");
        }

        return xreView.getWidth();
    }

    /**
     * Get resource's measured height.
     *
     * @return measured height of resource
     */
    public int getMeasuredHeight() {
        if (xreView == null) {
            throw new IllegalStateException("Resource measured height is required, but it is not attached to XREView.");
        }

        return xreView.getHeight();
    }
}
